﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.HardWareSDK;
using ZK.InterfaceServer;

namespace ZK.HardWareBLL
{
    /// <summary>
    /// 菱声音响
    /// </summary>
    public class LingShengAudioBLL
    {
        public string Ip { get; set; }
        public int Port { get; set; }
        public LingShengAudioBLL()
        {
            Common.HaredWareCommonM lengSheng = Common.IConfig.GetSetLengSheng;
            this.Ip = lengSheng.Ip;
            this.Port = Convert.ToInt32(lengSheng.Port);
            TestConnect.CheckDeviceStatus(lengSheng.Ip, lengSheng.Name);
        }
        /// <summary>
        /// ***弃用
        /// </summary>
        /// <param name="taskId"></param>
        //public LingShengAudio(int taskId)
        //{
        //    Common.HaredWareCommonM lengSheng = Common.IConfig.GetSetLengSheng;
        //    this.Ip = lengSheng.Ip;
        //    this.Port = Convert.ToInt32(lengSheng.Port);
        //    TestConnect.CheckDeviceStatus(lengSheng.Ip, lengSheng.Name);
        //    AutoMaticLogin();
        //    StartTask(taskId);
        //    DisConnect();
        //}

        /// <summary>
        /// 自动化播放文件任务
        /// </summary>
        /// <param name="taskId"></param>
        public void AutoStartTask(int taskId)
        {
            AutoMaticLogin();
            StartTask(taskId);
            DisConnect();
        }
        /*
         connect with ip and port
         show 1 of result is success
         */
        public int Connect()
        {
            return LingShengAudioSdk.audio_OpenAudioCast(this.Ip, this.Port);
        }

        //get task list detial 任务名称|定时使能|开始时间|开始日期|任务ID|周列表|频道编号|
        public string GetTaskList()
        {
            AutoMaticLogin();
            string res = LingShengAudioSdk.audio_GetTaskList();
            DisConnect();
            return res;
        }
        /*
         set event
             */
        public int SetEvent()
        {
            return LingShengAudioSdk.audio_SetEventFunc(0xff, onTAudioCastEvent);
        }
        /*
         onTAudioCastEvent
             */
        public int onTAudioCastEvent(int eventMsg, string parstr)
        {
            //e_msg = eventMsg;
            //str_par = parstr;
            //left_time = 1;
            //show_text3 = show_text3 + "eventMsg[" + e_msg + "], parstr[" + str_par + "]\r\n";
            return 1;
        }
        public string Login()
        {
            return LingShengAudioSdk.audio_CheckPWD("admin", ""); ;
        }
        public int DisConnect()
        {
            return LingShengAudioSdk.audio_CloseAudioCast();
        }
        /*
         start task
             */
        public void StartTask(int taskId)
        {
            LingShengAudioSdk.audio_StartTask(taskId);
        }
        //public string StartTask(int taskId)
        //{
        //    return HardWareBLL.LingShengAudioSdk.audio_StartTask(taskId);
        //}
        /*
         stop task
             */
        public string StopTask(int taskId)
        {
            return LingShengAudioSdk.audio_StopTask(taskId);
        }
        /*  
         set volume
             */
        public int SetVolume(int wareId, int volume)
        {
            return LingShengAudioSdk.audio_SetVolume(wareId, volume);
        }

        /*
         automatic login 
         include connect-setevent-login
             */
        public int AutoMaticLogin()
        {
            int res;
            string strRes;
            res = Connect();
            if (res != 1)
            {
                Common.Log4.LogManage.WriteErr("菱声音响系统服务连接失败");
                Common.ShowBlackBox.WriteLine("菱声音响系统服务连接失败");
                return res;
            }
            res = SetEvent();
            if (res != 1)
            {
                Common.Log4.LogManage.WriteErr("菱声音响系统服务设置事件失败。");
                Common.ShowBlackBox.WriteLine("菱声音响系统服务设置事件失败");
                return res;
            }
            strRes = Login();
            if (string.IsNullOrEmpty(strRes))
            {
                Common.Log4.LogManage.WriteErr("菱声音响系统登录：" + strRes);
                Common.ShowBlackBox.WriteLine("菱声音响系统登录：" + strRes);
                res = 0;
            }
            else
            {
                Common.ShowBlackBox.WriteLine("自动登录完成");
            }

            return res;

        }
    }
}
